package com.tnovoselec.android.mojevijesti.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.app.SherlockFragment;
import com.tnovoselec.android.mojevijesti.R;
import com.tnovoselec.android.mojevijesti.adapter.ChannelsAdapter;
import com.tnovoselec.android.mojevijesti.adapter.NewsFragmentAdapter;
import com.tnovoselec.android.mojevijesti.async.GetChannelsAsync;
import com.tnovoselec.android.mojevijesti.async.GetChannelsAsync.ChannelsDownloadedListener;
import com.tnovoselec.android.mojevijesti.async.GetNewsAsync;
import com.tnovoselec.android.mojevijesti.async.GetNewsAsync.NewsDownloadedListener;
import com.tnovoselec.android.mojevijesti.config.Config;
import com.tnovoselec.android.mojevijesti.dto.ChannelDto;
import com.tnovoselec.android.mojevijesti.dto.ChannelsResultHolder;
import com.tnovoselec.android.mojevijesti.dto.NewsResultHolder;

public class HomeFragment extends SherlockFragment implements ChannelsDownloadedListener, OnNavigationListener, NewsDownloadedListener {

	private ViewPager viewpager;
	private NewsFragmentAdapter adapter;
	private ChannelsAdapter chAdapter;
	private ChannelDto activeChannel;
	

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		GetChannelsAsync gca = new GetChannelsAsync(Config.MAIN_URL, Config.PATH_TO_SAVE, false, getActivity());
		gca.setListener(this);
		gca.execute(new Void[0]);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.activity_main, container, false);
		viewpager = (ViewPager) v.findViewById(R.id.main_viewpager);
		return v;
	}

	@Override
	public void onChannelsDownloaded() {

		chAdapter = new ChannelsAdapter(getActivity());
		ActionBar actionBar = getSherlockActivity().getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		actionBar.setListNavigationCallbacks(chAdapter, this);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayUseLogoEnabled(false);
		actionBar.setDisplayShowHomeEnabled(false);
	}

	@Override
	public boolean onNavigationItemSelected(int itemPosition, long itemId) {
		activeChannel = ChannelsResultHolder.getAll().get(itemPosition);
		chAdapter.setActiveChannel(itemPosition);
		NewsResultHolder.clear();
		GetNewsAsync gna = new GetNewsAsync(activeChannel, Config.PATH_TO_SAVE, false, getActivity());
		gna.setListener(this);
		gna.execute(new Void[0]);
		return true;
	}

	@Override
	public void onNewsDownloaded() {

		adapter = new NewsFragmentAdapter(getFragmentManager(), activeChannel);
		viewpager.setAdapter(adapter);

	}

}
